package com.jingsheng.jingshengcity.mvp.ui.activity;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.jingsheng.jingshengcity.R;
import com.jingsheng.jingshengcity.di.component.DaggerMainComponent;
import com.jingsheng.jingshengcity.di.module.MainModule;
import com.jingsheng.jingshengcity.mvp.contract.MainContract;
import com.jingsheng.jingshengcity.mvp.presenter.MainPresenter;
import com.jingsheng.jingshengcity.mvp.ui.fragment.tab.ClassifyFragment;
import com.jingsheng.jingshengcity.mvp.ui.fragment.tab.DiscoverFragment;
import com.jingsheng.jingshengcity.mvp.ui.fragment.tab.HomeFragment;
import com.jingsheng.jingshengcity.mvp.ui.fragment.tab.MineFragment;
import com.jingsheng.jingshengcity.mvp.ui.fragment.tab.ShoppingCartFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * Created by Administrator on 2017/5/5.
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    @NonNull
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    @NonNull
    @BindView(R.id.bottom_bar)
    BottomBar mBottomBar;

    private BaseFragment currentFragment;
    private HomeFragment mHomeFragment;
    private ClassifyFragment mClassifyFragment;
    private DiscoverFragment mDiscoverFragment;
    private ShoppingCartFragment mShoppingCartFragment;
    private MineFragment mMineFragment;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent
                .builder()
                .appComponent(appComponent)
                .mainModule(new MainModule(this)) //请将MainModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    public int initView() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        if (mHomeFragment == null) {
            mHomeFragment = HomeFragment.newInstance();
        }
        if (!mHomeFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.fl_content, mHomeFragment).commitAllowingStateLoss();
        } else {
            getSupportFragmentManager().beginTransaction().show(mHomeFragment).commitAllowingStateLoss();
        }
        currentFragment = mHomeFragment;

        mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_home) {
                    clickHomeLayout();
                } else if (tabId == R.id.tab_classify) {
                    clickClassifyLayout();
                } else if (tabId == R.id.tab_discover) {
                    clickDiscoverLayout();
                } else if (tabId == R.id.tab_shoppingcart) {
                    clickShoppingcartLayout();
                } else if (tabId == R.id.tab_mine) {
                    clickMineLayout();
                }
            }
        });
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        UiUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private void clickHomeLayout() {
        if (mHomeFragment == null) {
            mHomeFragment = HomeFragment.newInstance();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), mHomeFragment);
    }

    private void clickClassifyLayout() {
        if (mClassifyFragment == null) {
            mClassifyFragment = ClassifyFragment.newInstance();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), mClassifyFragment);
    }

    private void clickDiscoverLayout() {
        if (mDiscoverFragment == null) {
            mDiscoverFragment = DiscoverFragment.newInstance();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), mDiscoverFragment);
    }

    private void clickShoppingcartLayout() {
        if (mShoppingCartFragment == null) {
            mShoppingCartFragment = ShoppingCartFragment.newInstance();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), mShoppingCartFragment);
    }

    private void clickMineLayout() {
        if (mMineFragment == null) {
            mMineFragment = MineFragment.newInstance();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), mMineFragment);
    }

    protected void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
        if (currentFragment == fragment)
            return;

        if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
            transaction.hide(currentFragment).add(R.id.fl_content, fragment).commitAllowingStateLoss();
        } else {
            transaction.hide(currentFragment).show(fragment).commitAllowingStateLoss();
        }

        currentFragment = (BaseFragment) fragment;
    }

}